<template>
  <el-dialog
    :visible.sync="value"
    :before-close="cancel"
    width="40%"
  >
    <template slot="title">
      本月佣金链
    </template>
    <ModalWrapper :min-width="450">
      <el-form>
        <el-form-item label="模式:" prop="name" v-if="!isFirstLevel">
          <div class="aden-comm-mode-wrapper-view">
            <div class="header-view">
              <span
                :style="getModeStyle()"
              >
                {{isCash ? '现金网': '信用网'}}
              </span>
            </div>
            <div
              v-for="(item, index) in itemList"
            >
              <div
                class="chart-item-view segment-item-view"
                v-if="index !== 0"
              >
                <div class="left-view">
                  <span></span>
                </div>
                <div class="right-view" v-if="!isCash">+</div>
              </div>
              <div class="chart-item-view">
                <div class="left-view">
                  <span>{{index + 1}}</span>
                  <div>{{item.proxyName}}</div>
                </div>
                <div class="right-view">
                  <div>{{item.rate}}%</div>
                  <span v-if="isCash">*</span>
                </div>
              </div>
            </div>
          </div>
        </el-form-item>

        <div
          v-if="isFirstLevel"
          class="first-level-view"
        >
          <div class="first-level-header-view">当前账户已是1级代理，无佣金链配置。</div>
          <div class="first-level-item-view">
            <div>{{rowData.name}}</div>
            <div>{{tableData.cashRuleName}}</div>
          </div>
        </div>

        <el-table
          :data="tableData.cashRuleDetail"
          border
          style="width: 100%"
          v-if="isCash || isFirstLevel"
        >
          <el-table-column
            prop="profit"
            label="公司总输赢"
            width="180"
            align="center"
          >
            <template slot-scope="{row}">
              >= &nbsp;{{row.profit || 0}}
            </template>
          </el-table-column>
          <el-table-column
            prop="userCount"
            label="有效会员数"
            width="180"
            align="center"
          >
            <template slot-scope="{row}">
              >= &nbsp;{{row.userCount || 0}}
            </template>
          </el-table-column>
          <el-table-column
            prop="commission"
            label="会员佣金"
            align="center"
          >
            <template slot-scope="{row}">
              {{row.commission || 0}} %
            </template>
          </el-table-column>
        </el-table>

        <el-table
          :data="creditList"
          border
          style="width: 100%"
          v-else
        >
          <el-table-column
            prop="userCount"
            label="有效会员数"
            align="center"
          >
            <template slot-scope="{row}">
              >= &nbsp;{{row.userCount || 0}}
            </template>
          </el-table-column>
          <el-table-column
            prop="commission"
            label="会员总佣金"
            align="center"
          >
            <template slot-scope="{row}">
              {{row.commission || 0}} %
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </ModalWrapper>
  </el-dialog>
</template>

<script>

// utils
import { formatCommMode } from '@/utils/base';

// components
import ModalWrapper from '@/components/ModalWrapper/index';

// mixins
import { grid, dialog } from '@/build';

// services
import { getProxyTreeDetails } from '@/services/api/agency';

export default {
  name: 'agency-tree-details-dialog',
  props: ['value', 'id', 'rowData'],
  mixins: [grid, dialog],
  components: {
    ModalWrapper,
  },
  data() {
    return {
      name: null,
      tableData: {
        cashRuleDetail: [],
        commLine: {},
      },
    };
  },
  computed: {
    isCash() {
      return this.rowData.mode === 1;
    },
    isFirstLevel() {
      return this.rowData.mode === 0;
    },
    cashRuleDetail() {
      return this.tableData.cashRuleDetail || [];
    },
    itemList() {
      return this.tableData.commLine.itemList || [];
    },
    creditList() {
      const {
        commLine: {
          itemList = [],
          minEffectMemberNum = 0,
        },
      } = this.tableData;
      if (!itemList.length) return [{ userCount: 0, commission: 0 }];
      const commission = itemList.reduce((total, curr) => (total + Number(curr.rate)), 0);
      return [{ userCount: minEffectMemberNum, commission }];
    },
  },
  mounted() {
    if (this.id) {
      this.bindGet(getProxyTreeDetails, 'tableData', {
        sendingData: { uid: this.id },
      });
      this.getAll();
    }
  },
  methods: {
    cancel() {
      this.$emit('input', false);
    },
    getModeStyle() {
      return formatCommMode(this.rowData.mode).style;
    },
  },

};
</script>

<style lang="scss">
  .first-level-view {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .first-level-header-view {
      padding-bottom: 5px;
      font-weight: 700;
    }

    .first-level-item-view {
      width: 100%;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      > div {
        text-align: center;
        font-size: 15px;
        font-weight: 700;
      }

      >div:last-child {
        padding-left: 20px;
      }
    }
  }

  .aden-comm-mode-wrapper-view {
      display: flex;
      flex-direction: column;
      .header-view > span {
        padding: 3px 4px;
        border-radius: 4px;
        color: #ffffff;
      }

      .chart-item-view {
        display: flex;
        align-items: center;
        min-height: 28px;

        .left-view, .right-view {
          display: flex;
          align-items: center;
          font-weight: 700;
        }

        .left-view {
          min-width: 100px;
          > div {
            margin-left: 7px;
          }
        }

        .right-view > span {
          padding-left: 10px;
          padding-top: 4px;
          font-size: 18px;
        }
      }

      .segment-item-view {
        .left-view, .right-view {
          height: 50px;
        }

        .left-view {
          padding-left: 20px;
          >span {
            margin-left: 15px;
            height: 100%;
            width: 1px;
            background-color: rgba(0, 0, 0, .6);
          }
        }

        .right-view {
          padding-left: 6px;
        }
      }
    }
</style>
